﻿using System;
using System.Data;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using HiauPdm.Bll;
using HiauPdm.Web;

namespace HiauPdm.Web
{
    public partial class UserManager : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.MaintainScrollPositionOnPostBack = true;
            if (!Page.IsPostBack)
            {
                Page.MaintainScrollPositionOnPostBack = true;
                BtnSave.OnClientClick = "this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(BtnSave, null) + ";return false;";
                BindData();
                TextUser.Focus();
            }
        }

        private void BindData()
        {
            DataSet ds = BMain.GetUsers();
            if ((ds == null) || (ds.Tables[0].Rows.Count <= 0))
            {
                PanelList.InnerHtml = "<table cellspacing='0' cellpadding='0' border='0' style='border-style:none;border-collapse:collapse;width:832px;text-align:center;'><tr><td>[未找到用户记录]</td></tr></table>";
            }
            else
            {
                GridCpSj.DataSource = ds;
                GridCpSj.DataBind();
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            if (!ShowErr(BMain.SaveUser((GridCpSj.SelectedValue == null ? 0 : (int)GridCpSj.SelectedValue), TextUser.Text, TextRealName.Text, TextSex.Text, TextTel.Text, TextBz.Text, (string)Session["RealName"], (GridCpSj.SelectedValue == null ? null : TreeModule.Nodes[0]))))
            {
                Response.Redirect(Request.Url.PathAndQuery);
            }
        }

        protected void TreeModule_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            DataSet dsModule = BMain.GetChildModule(int.Parse(e.Node.Value), (int)GridCpSj.SelectedValue);
            if (dsModule != null && dsModule.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in dsModule.Tables[0].Rows)
                {
                    TreeNode tn = new TreeNode(string.Format("[{0}]{1}", dr["模块编号"], dr["模块名称"]), dr["ID"].ToString());
                    tn.ToolTip = tn.Text;
                    tn.SelectAction = TreeNodeSelectAction.None;
                    tn.PopulateOnDemand = true;
                    tn.Checked = (bool)dr["模块选择"];
                    e.Node.ChildNodes.Add(tn);
                }
            }
        }

        protected void TreeModule_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
            if (e.Node.Checked)
            {
                CheckedParent(e.Node);
            }
            else
            {
                UnCheckedChilds(e.Node);
            }
        }

        private void CheckedParent(TreeNode tn)
        {
            tn.Checked = true;
            if (tn.Parent != null)
            {
                CheckedParent(tn.Parent);
            }
        }

        private void UnCheckedChilds(TreeNode tn)
        {
            tn.Checked = false;
            if (tn.ChildNodes != null && tn.ChildNodes.Count > 0)
            {
                for (int i = 0; i < tn.ChildNodes.Count; i++)
                {
                    UnCheckedChilds(tn.ChildNodes[i]);
                }
            }
        }

        private void CheckedChilds(TreeNode tn)
        {
            tn.Checked = true;
            if (tn.ChildNodes != null && tn.ChildNodes.Count > 0)
            {
                for (int i = 0; i < tn.ChildNodes.Count; i++)
                {
                    CheckedChilds(tn.ChildNodes[i]);
                }
            }
        }

        protected void BtnSelectAll_Click(object sender, EventArgs e)
        {
            CheckedChilds(TreeModule.Nodes[0]);
        }

        protected void BtnUnSelectAll_Click(object sender, EventArgs e)
        {
            UnCheckedChilds(TreeModule.Nodes[0]);
        }

        protected void GridCpSj_SelectedIndexChanged(object sender, EventArgs e)
        {
            TextUser.Text = GridCpSj.SelectedRow.Cells[1].Text;
            TextRealName.Text = GridCpSj.SelectedRow.Cells[2].Text.Replace("&nbsp;", "");
            TextSex.Text = GridCpSj.SelectedRow.Cells[3].Text.Replace("&nbsp;", "");
            TextTel.Text = GridCpSj.SelectedRow.Cells[4].Text.Replace("&nbsp;", "");
            TextBz.Text = GridCpSj.SelectedRow.Cells[5].Text.Replace("&nbsp;", "");
            TreeNode tn = new TreeNode("系统菜单", "0");
            tn.SelectAction = TreeNodeSelectAction.None;
            tn.PopulateOnDemand = true;
            tn.Checked = true;
            TreeModule.Nodes.Clear();
            TreeModule.Nodes.Add(tn);
            TreeModule.ExpandAll();
            TextUser.ReadOnly = true;
            TextUser.BackColor = Color.FromArgb(0xE7F3FF);
            PanelUserRights.Visible = true;
            BtnSave.Text = BtnSave.ToolTip = "保存";
            BtnRefresh.Value = BtnRefresh.Attributes["title"] = "取消";
            for (int i = 0; i < GridCpSj.Rows.Count; i++)
            {
                GridCpSj.Rows[i].Cells[6].Controls[3].Visible = false;
                GridCpSj.Rows[i].Cells[6].Controls[5].Visible = false;
            }
            TextRealName.Focus();
        }

        protected void GridCpSj_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (!ShowErr(BMain.DelUser((int)GridCpSj.DataKeys[e.RowIndex].Value)))
            {
                Response.Redirect(Request.Url.PathAndQuery);
            }
        }

        protected void GridCpSj_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ResetPwd")
            {
                if (!ShowErr(BMain.ResetPwd((int)GridCpSj.DataKeys[Convert.ToInt32(e.CommandArgument)].Value, GridCpSj.DataKeys[Convert.ToInt32(e.CommandArgument)].Values[1].ToString(), (string)Session["RealName"])))
                {
                    Response.Redirect(Request.Url.PathAndQuery);
                }
            }
        }
    }
}